package com.topscomm.pm.script;

import cn.hutool.core.date.DateField;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.pm.PmScript;
import com.topscomm.pm.pojo.PmPauseApplyEntity;
import com.topscomm.pm.pojo.PmProjectEntity;
import com.topscomm.pm.pojo.PmRecoveryApplyEntity;
import com.topscomm.pm.service.impl.PmCalendarService;
import com.topscomm.pm.service.impl.PmPauseApplyService;
import com.topscomm.pm.service.impl.PmProjectService;
import com.topscomm.pm.service.impl.PmRecoveryApplyService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.DateUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PmRecoveryScript extends PmScript {
    /**
     * @author: wushenghong
     * @date: 2020/6/4
     * @description: 将延期时间更新至项目档案，并生成项目日历
     */
    @Transactional
    public void updateProjectAndCalendar(ITapService service, TapEntity entity) throws Exception {
        // 审批节点实例
        CboApprovalNodeInsEntity nodeIns = (CboApprovalNodeInsEntity) entity;
        String sourceId = ConvertUtil.convertToString(nodeIns.getSourceid());
        PmProjectService pmProjectService = (PmProjectService) SpringUtil.getBean("pmProjectService");
        PmRecoveryApplyService pmRecoveryApplyService = (PmRecoveryApplyService) SpringUtil.getBean("pmRecoveryApplyService");
        PmCalendarService pmCalendarService = (PmCalendarService) SpringUtil.getBean("pmCalendarService");
        Date today = DateUtil.getZreoOfDay(new Date());
        PmRecoveryApplyEntity pmRecoveryApplyEntity = pmRecoveryApplyService.queryById(sourceId);
        if (pmRecoveryApplyEntity != null) {
            long projectId = pmRecoveryApplyEntity.getProjectid();//项目id
            Date nowPlanEndTime = pmRecoveryApplyEntity.getEndtime();//新的完工时间
            Date oldPlanEndTimeTemp = pmRecoveryApplyEntity.getPlanendtimeold();//旧的完工时间
            Date oldPlanEndTime = oldPlanEndTimeTemp == null ? null : cn.hutool.core.date.DateUtil.offset(oldPlanEndTimeTemp, DateField.DAY_OF_MONTH, 1);
            PmProjectEntity pmProjectEntity = pmProjectService.queryById(ConvertUtil.convertToString(projectId));
            String projectCode = pmProjectEntity == null ? "" : pmProjectEntity.getCode();//项目号
            Map<String, Object> map = new HashMap<>();
            if (pmProjectEntity != null) {
                // 更新项目计划完工时间、项目状态
                pmProjectEntity.setPlanendtime(nowPlanEndTime);
                pmProjectEntity.setProjectstate(2);
                pmProjectService.updateEntity(pmProjectEntity);
            }
            // 同时更新项目日历
            map.put("projectid", projectId);
            map.put("projectcode", projectCode);
            map.put("starttime", oldPlanEndTime);
            map.put("endtime", nowPlanEndTime);
            pmCalendarService.insertBatchCalendar(map);
            // 更新项目恢复时间
            pmRecoveryApplyEntity.setRecoverytime(today);
            pmRecoveryApplyService.updateEntity(pmRecoveryApplyEntity);
            //更新项目暂停恢复时间
            PmPauseApplyService pmPauseApplyService = (PmPauseApplyService) SpringUtil.getBean("pmPauseApplyService");
            StringBufferProxy sql = new StringBufferProxy();
            sql.appendSingle(" projectid = {0}", projectId);
            List<PmPauseApplyEntity> pmPauseApplyEntityList = pmPauseApplyService.queryByWhere(sql.toString());
            if (pmPauseApplyEntityList.size() > 0) {
                PmPauseApplyEntity pmPauseApplyEntity = pmPauseApplyEntityList.get(0);
                pmPauseApplyEntity.setEndtime(today);
                pmPauseApplyEntity.setDocstate("2");//1暂停中 2已恢复
                pmPauseApplyService.updateEntity(pmPauseApplyEntity);
            }
        }
    }
}
